﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using David.Framework.Core.Extensions;

namespace David.Framework.Core.Helper
{
    public class Check
    {
        internal Check()
        {
        }

        public class Argument
        {
            internal Argument()
            {
            }

            /// <summary>
            /// 参数不能为空
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotEmpty(Guid argument, string argumentName)
            {
                if (argument == Guid.Empty)
                {
                    throw new ArgumentException("\"{0}\" 不能为空的Guid.".FormatWith(argumentName), argumentName);
                }
            }

            /// <summary>
            /// 参数不能为空
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotEmpty(string argument, string argumentName)
            {
                if (string.IsNullOrEmpty((argument ?? string.Empty).Trim()))
                {
                    throw new ArgumentException("\"{0}\" 不能为空字符串.".FormatWith(argumentName), argumentName);
                }
            }

            /// <summary>
            /// 参数是否为Guid
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsGuid(string argument, string argumentName)
            {
                var guid = new Guid();

                if (!Guid.TryParse(argument, out guid))
                {
                    throw new ArgumentException("\"{0}\" 字符串不符合Guid格式.".FormatWith(argumentName), argumentName);
                }
            }

            /// <summary>
            /// 参数长度不符合要求
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="length"></param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotOutOfLength(string argument, int length, string argumentName)
            {
                if (argument.Trim().Length > length)
                {
                    throw new ArgumentException("\"{0}\" cannot be more than {1} character.".FormatWith(argumentName, length), argumentName);
                }
            }

            /// <summary>
            /// 参数不存在
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotNull(object argument, string argumentName)
            {
                if (argument == null)
                {
                    throw new ArgumentNullException(argumentName);
                }
            }


            /// <summary>
            /// 参数Guid不为空
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>

            [DebuggerStepThrough]
            public static void IsNotNullGuid(Guid argument, string argumentName)
            {
                if (argument == Guid.Empty)
                {
                    throw new ArgumentException("\"{0}\" Guid值不能为空.".FormatWith(argumentName), argumentName);
                }

            }

            /// <summary>
            /// 参数不能小于零
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotNegative(int argument, string argumentName)
            {
                if (argument < 0)
                {
                    throw new ArgumentOutOfRangeException(argumentName);
                }
            }

            /// <summary>
            /// 参数是不能小于等于零
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotNegativeOrZero(int argument, string argumentName)
            {
                if (argument <= 0)
                {
                    throw new ArgumentOutOfRangeException(argumentName);
                }
            }

            /// <summary>
            /// 参数不能小于零
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotNegative(long argument, string argumentName)
            {
                if (argument < 0)
                {
                    throw new ArgumentOutOfRangeException(argumentName);
                }
            }

            /// <summary>
            /// 参数是不能小于等于零
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotNegativeOrZero(long argument, string argumentName)
            {
                if (argument <= 0)
                {
                    throw new ArgumentOutOfRangeException(argumentName);
                }
            }

            /// <summary>
            /// 参数不能小于零
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotNegative(float argument, string argumentName)
            {
                if (argument < 0)
                {
                    throw new ArgumentOutOfRangeException(argumentName);
                }
            }

            /// <summary>
            /// 参数是不能小于等于零
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotNegativeOrZero(float argument, string argumentName)
            {
                if (argument <= 0)
                {
                    throw new ArgumentOutOfRangeException(argumentName);
                }
            }

            /// <summary>
            /// 参数不能小于零
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotNegative(decimal argument, string argumentName)
            {
                if (argument < 0)
                {
                    throw new ArgumentOutOfRangeException(argumentName);
                }
            }

            /// <summary>
            /// 参数是不能小于等于零
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotNegativeOrZero(decimal argument, string argumentName)
            {
                if (argument <= 0)
                {
                    throw new ArgumentOutOfRangeException(argumentName);
                }
            }

            /// <summary>
            /// 参数是不能小于等于零
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotNegativeOrZero(double argument, string argumentName)
            {
                if (argument <= 0)
                {
                    throw new ArgumentOutOfRangeException(argumentName);
                }
            }

            /// <summary>
            /// 参数不能小于零
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotNegative(TimeSpan argument, string argumentName)
            {
                if (argument < TimeSpan.Zero)
                {
                    throw new ArgumentOutOfRangeException(argumentName);
                }
            }

            /// <summary>
            /// 参数不能小于等于零
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotNegativeOrZero(TimeSpan argument, string argumentName)
            {
                if (argument <= TimeSpan.Zero)
                {
                    throw new ArgumentOutOfRangeException(argumentName);
                }
            }

            /// <summary>
            /// 参数不能为空
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotEmpty<T>(ICollection<T> argument, string argumentName)
            {
                IsNotNull(argument, argumentName);

                if (argument.Count == 0)
                {
                    throw new ArgumentException("Collection cannot be empty.", argumentName);
                }
            }

            /// <summary>
            /// 参数不在规定范围内
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="min">最小参数</param>
            /// <param name="max">最大参数</param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotOutOfRange(int argument, int min, int max, string argumentName)
            {
                if ((argument < min) || (argument > max))
                {
                    throw new ArgumentOutOfRangeException(argumentName, "{0} must be between \"{1}\"-\"{2}\".".FormatWith(argumentName, min, max));
                }
            }

            /// <summary>
            /// 参数不为Email格式
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotInvalidEmail(string argument, string argumentName)
            {
                IsNotEmpty(argument, argumentName);

                if (!argument.IsEmail())
                {
                    throw new ArgumentException("\"{0}\" is not a valid email address.".FormatWith(argumentName), argumentName);
                }
            }

            /// <summary>
            /// 参数不是有效的WEB URL格式
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotInvalidWebUrl(string argument, string argumentName)
            {
                IsNotEmpty(argument, argumentName);

                if (!argument.IsWebUrl())
                {
                    throw new ArgumentException("\"{0}\" is not a valid web url.".FormatWith(argumentName), argumentName);
                }
            }


            /// <summary>
            /// 参数集合长度不小于等于零个
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>

            [DebuggerStepThrough]
            public static void IsNotCountLessThanZero(IEnumerable<int> argument, string argumentName)
            {
                IsNotNull(argument, "argument");

                if (argument.Count() <= 0)
                {
                    throw new ArgumentException("\"{0}\" 数组元素不能小于等于零个.".FormatWith(argumentName), argumentName);

                }

            }
            /// <summary>
            /// 参数集合长度不小于等于零个
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>

            [DebuggerStepThrough]
            public static void IsNotNullAndCountLessThanZero(IEnumerable<int> argument, string argumentName)
            {
                IsNotNull(argument, "argument");

                if (argument.Count() <= 0)
                {
                    throw new ArgumentException("\"{0}\" 数组元素不能小于等于零个.".FormatWith(argumentName), argumentName);
                }

            }
            /// <summary>
            /// 参数集合长度不小于等于零个
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>

            [DebuggerStepThrough]
            public static void IsNotNullAndCountLessThanZero(IEnumerable<string> argument, string argumentName)
            {
                IsNotNull(argument, "argument");

                if (argument.Count() <= 0)
                {
                    throw new ArgumentException("\"{0}\" 数组元素不能小于等于零个.".FormatWith(argumentName), argumentName);
                }
            }
            /// <summary>
            /// 参数集合长度不小于等于零个
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>

            [DebuggerStepThrough]
            public static void IsNotNullAndCountLessThanZero(IEnumerable<long> argument, string argumentName)
            {
                IsNotNull(argument, "argument");

                if (argument.Count() <= 0)
                {
                    throw new ArgumentException("\"{0}\" 数组元素不能小于等于零个.".FormatWith(argumentName), argumentName);
                }
            }


        }
    }
}
